Skip to content

Swap in PyTorch kernels for MHA#13

Closed
wadkisson wants to merge 3 commits into
lean-dojo:mainfrom
wadkisson:attention-pr
Closed

Swap in PyTorch kernels for MHA#13
wadkisson wants to merge 3 commits into
lean-dojo:mainfrom
wadkisson:attention-pr

Conversation

@wadkisson

Copy link
Copy Markdown
Collaborator

In the current cuda training process, the multi headed attention mechanism is the biggest bottleneck to training transformer models. Currently, training a ~500M parameter GPT model is ~74x as slow as PyTorch.

In this PR, I swap the actual computation of the MHA Q,K,V scores to be done entirely with PyTorch's kernels, Libtorch. This implantation more than doubles the speed of training large GPT models in TorchLean — the same ~500M parameter model ran ended up being ~31x as slow as PyTorch after this fix.

The diff is quite scoped and does three things: Adds Libtorch as a requirement for training TorchLean with cuda, and wires in libtorch for computation of the forward attention mechanism as well as the backward attention mechanism.

Let me know if any changes are needed. Thanks!

@Robertboy18

Copy link
Copy Markdown
Member

Merged into main as part of the consolidated backend work. Thanks, Will! Closing this PR.

@wadkisson wadkisson deleted the attention-pr branch July 11, 2026 19:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants